package kt;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import kt.dal.DAL;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * Example class to submit a message to a SMSC using synchronous communication.
 * This class simply binds to the server, submits a message, and then unbinds.
 * 
 * @see ie.omk.smpp.examples.ParseArgs ParseArgs for details on running this
 *      class.
 */
public class SelectWinner1765Job implements Job {
	//private Log logger = LogFactory.getLog(SendMTJob.class);
    private static final Logger logger = LogManager.getLogger(SelectWinner1765Job.class);
   
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		java.sql.Connection conn = null;
		String strSQL = "select DISTINCT ISDN from LA_SESSION where SESSION_START_TIME > DATE_ADD(now(),INTERVAL -7 DAY) "
			+ " AND NO_OF_QUES_ANS = 2 "
			+ " ORDER BY RAND() LIMIT 15";
		try {
			// logger.info("get MT of " + operator);
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost/services?autoReconnect=true",
					"root", "admin123ewq");
			String re = "";			
			PreparedStatement preStmt = conn.prepareStatement(strSQL);
			ResultSet rs = preStmt.executeQuery();
			while (rs.next()) {
				re += ", " + rs.getString(1);
			}			
			if(re.length() > 0) {
				re = re.replaceAll(",855", "");				
				//DAL.saveMT_Metfone("855979066460", "10 winners of 1765: " + re, "1765");
				DAL.saveMT_Metfone("855977900068", "winners: " + re, "1765");
				DAL.saveMT_Metfone("855977500243", "winners: " + re, "1765");
				DAL.saveMT_Metfone("855975252526", "winners: " + re, "1765");
				DAL.saveMT_Metfone("855886335111", "winners: " + re, "1765");
				DAL.saveMT_Metfone("855979066460", "winners: " + re, "1765");
			} else {
				DAL.saveMT_Metfone("855979066460", "15 winners of 1765: No one" + re, "1765");
			}
			rs.close();
			preStmt.close();
			
		} catch (Exception e) {
			logger.error("Error executing SQL " + strSQL, e);
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					logger.error("error when close the conn", e);
				}
			}
		}
	}
}

